package com.xiaojie.learn.util;

import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class ConsistencyHashing {

    public static void main(String[] args) {
        //模拟客户端的请求
        String[] nodes = { "127.0.0.1", "10.9.3.253", "192.168.10.1" };

        for (String node : nodes) {
            System.out.println("[" + node + "]的hash值为" + getHash(node) + ", 被路由到结点[" + getServer(node) + "]");
        }

        // 添加一个节点(模拟服务器上线)
        addNode("192.168.1.7");
        // 删除一个节点（模拟服务器下线）
        delNode("192.168.1.2");

        for (String node : nodes) {
            System.out.println("[" + node + "]的hash值为" + getHash(node) + ", 被路由到结点[" + getServer(node) + "]");
        }
    }

    //虚拟节点的个数
    private static final int VIRTUAL_NUM = 5;
    //虚拟节点分配，key是hash值，value是虚拟节点服务器名称
    private static SortedMap<Integer,String> shards = new TreeMap<Integer, String>();
    //真实节点列表
    private static List<String> realNodes = new LinkedList<String>();
    //模拟初始服务器
    private static String[] servers = {"192.168.1.1","192.168.1.2","192.168.1.3","192.168.1.4","192.168.1.5"};

    static {
        for (String server : servers) {
            realNodes.add(server);
            System.out.println("真实节点[" + server + "] 被添加");
            addVirtualNode(server);
        }
    }

    //获取被分配的节点名
    public static String getServer(String node){
        int hash = getHash(node);
        Integer key = null;
        SortedMap<Integer,String> subMap = shards.tailMap(hash);
        if(subMap.isEmpty()){
            key = shards.lastKey();
        }else{
            key = subMap.firstKey();
        }
        String virtualNode = shards.get(key);
        return virtualNode.substring(0,virtualNode.indexOf("&&"));
    }

    //添加节点
    public static void addNode(String node){
        if(!realNodes.contains(node)){
            realNodes.add(node);
            System.out.println("真实节点[" + node + "] 上线添加");
            addVirtualNode(node);
        }
    }

    private static void addVirtualNode(String node) {
        for (int i = 0; i < VIRTUAL_NUM; i++) {
            String virtualNode = node + "&&VN" + i;
            int hash = getHash(virtualNode);
            shards.put(hash, virtualNode);
            System.out.println("虚拟节点[" + virtualNode + "] hash:" + hash + "，被添加");
        }
    }

    //删除节点
    public static void delNode(String node) {
        if (realNodes.contains(node)) {
            realNodes.remove(node);
            System.out.println("真实节点[" + node + "] 下线移除");
            delVirtualNode(node);
        }
    }

    private static void delVirtualNode(String node) {
        for (int i = 0; i < VIRTUAL_NUM; i++) {
            String virtualNode = node + "&&VN" + i;
            int hash = getHash(virtualNode);
            shards.remove(hash);
            System.out.println("虚拟节点[" + virtualNode + "] hash:" + hash + "，被移除");
        }
    }

    //FNV1_32_HASH算法
    private static int getHash(String str) {
        final int p = 16777619;
        int hash = (int) 2166136261l;
        for (int i = 0; i < str.length(); i++) {
            hash = (hash ^ str.charAt(i)) * p;
        }
        hash += hash << 13;
        hash ^= hash >> 7;
        hash += hash << 3;
        hash ^= hash >> 17;
        hash += hash << 5;
        //如果算出来的值为负值则取其绝对值
        hash = hash < 0 ? Math.abs(hash) : hash;
        return hash;
    }
}
